package com.hdu.clj.fitness

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_daytask.*
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.text.SimpleDateFormat
import java.util.*

class DaytaskActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_daytask)
        ActivityCollector.addActivity(this)

        val startIcon = Typeface.createFromAsset(assets,"iconfont.ttf")
        card2.setTypeface(startIcon)
        card2.setText(R.string.card2)
//        card3.setTypeface(startIcon)
//        card3.setText(R.string.card3)


        val dbHelper = userDatabase(this,"Users.db",1)
        val db = dbHelper.writableDatabase


        var ver = 1

        if (load("ver").isNotEmpty()){
            ver = load("ver").toInt()
        }
        val curUserName = load("userName")
        val curUserAcc = load("userAcc")

        val dbDayNum = DayNumDatabase(this,"Fit.db",ver,"abc"+curUserAcc.toString())
        val dbDay = dbDayNum.writableDatabase
        val dataName = "abc"+curUserAcc
        val date = SimpleDateFormat("yyyy/MM/dd").format(Date()).toString()
        var cardNum = 0

        var curUserData = db.rawQuery("select * from Users where acc = ?", arrayOf(curUserAcc.toString()))
        if (curUserData.moveToFirst()){
            cardNum = curUserData.getInt(curUserData.getColumnIndex("cardNum"))
        }
        //显示打卡天数
        day.setText(cardNum.toString())

        var curUserDayData = dbDay.rawQuery("select * from $dataName where date = ?", arrayOf(date))
        if (curUserDayData.moveToFirst()) {
            val cur_card = curUserDayData.getInt(curUserDayData.getColumnIndex("card"))
            Log.d("curdata",cur_card.toString())
            if (cur_card == 1) {
                dayFin.setText("已打卡")
                dayFin.setBackgroundColor(Color.parseColor("#CECFD8"))
                dayFin.isClickable = false
                dayFin.isEnabled = false
            } else {
                dayFin.setText("打卡")
                dayFin.setBackgroundColor(Color.parseColor("#4CAF50"))
                dayFin.isClickable = true
                dayFin.isEnabled = true
            }
        }

        //打卡
        dayFin.setOnClickListener {
            var curUserData = db.rawQuery("select * from Users where acc = ?", arrayOf(curUserAcc.toString()))
            if (curUserData.moveToFirst()){
                cardNum = curUserData.getInt(curUserData.getColumnIndex("cardNum"))
            }
            var curUserDayData = dbDay.rawQuery("select * from $dataName where date = ?", arrayOf(date))
            if (curUserDayData.moveToFirst()){
                //获取今天的运动情况
                val cur_card = curUserDayData.getInt(curUserDayData.getColumnIndex("card"))
                if (cur_card==0){
                    dbDay.execSQL("update $dataName set card = ? where date = ?", arrayOf(1,date))
                    db.execSQL("update Users set cardNum = ? where acc = ?", arrayOf(cardNum+1,curUserAcc))
                    day.setText((cardNum+1).toString())
                    dayFin.setText("已打卡")
                    dayFin.setBackgroundColor(Color.parseColor("#CECFD8"))
                    dayFin.isClickable = false
                    dayFin.isEnabled = false
                }
            }
        }

        //显示最多单日跳绳数
        var mostdayNum = 0
        var mostday = ""
        curUserDayData = dbDay.rawQuery("select max(num) from $dataName", null)
        if (curUserDayData.moveToFirst()) {
            mostdayNum = curUserDayData.getInt(0)
        }
        mostDayNum.setText(mostdayNum.toString())

        //显示最多跳绳数当日日期
        curUserDayData = dbDay.rawQuery("select date from $dataName where num=?", arrayOf(mostdayNum.toString()))
        if (curUserDayData.moveToFirst()) {
            mostday = curUserDayData.getString(0)
        }
        mostDay.setText(mostday)
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityCollector.removeActivity(this)
    }

    private fun load(data:String): String {
        val content = StringBuilder()

        try {
            val input = openFileInput(data)
            val reader = BufferedReader(InputStreamReader(input))
            reader.use {
                reader.forEachLine {
                    content.append(it)
                }
            }
        }catch (e: IOException){
            e.printStackTrace()
        }
        return content.toString()
    }
}
